package com.freedy.algorithm.kmp;

/**
 * 暴力匹配
 *
 * @author Freedy
 * @date 2021/4/12 16:11
 */
public class ViolenceMatch {
    public static void main(String[] args) {
        String a="I LOVE YOU SO MUCH";
        String b=" SO ";
        System.out.println(violenceMatch(a,b));
    }
    public static int violenceMatch(String str1, String str2) {
        char[] cs1 = str1.toCharArray();
        char[] cs2 = str2.toCharArray();
        int i=0;
        int j=0;
        while (i<cs1.length&&j<cs2.length){
            if (cs1[i]==cs2[j]){
                i++;
                j++;
            }else {
                i=i-(j-1);
                j=0;
            }
        }
        if (j==cs2.length){
            return i-j;
        }
        return -1;
    }
}
